//////////////////////////////////////
//// ASKIMA - COLLABORATOR PROFILE
//// MISSIONS MANAGEMENT FUNCTIONS
//////////////////////////////////////

// 1. DISPLAY MODE

/**
 * Expands a line in the mission table displaying associated skills
 */

function expandMission(sourceButton, missionID) {
	var nTr = sourceButton.parentNode.parentNode;
	if (sourceButton.src.match('details_close')) {
		/* This row is already open - close it */
		sourceButton.src = 'http://datatables.net/examples/examples_support/details_open.png';
		missionTable.fnClose(nTr);
	} else {
		/* Open this row */
		sourceButton.src = 'http://datatables.net/examples/examples_support/details_close.png';
		displayMissionDetails(missionTable, nTr, missionID);
	}
}

/**
 * Displays mission details (list of skills by default)
 */

function displayMissionDetails(oTable, nTr, missionID) {
	var aData = oTable.fnGetData(nTr);
	$.getJSON("missionSkillsToJSON", {
		missionID : missionID,
	}, function(data) {
		var sOut = "<ul>";
		$.each(data, function(i, val) {
			var name = val.name;
			sOut += "<li>" + name + "</li>";
		});
		sOut += "</ul>";
		missionTable.fnOpen(nTr, sOut, 'details');
	});
}

/**
 * Show the panel in order to add a mission
 */

function showAddMissionPanel() {

	// Creates a date picker in order to select the start date for the mission
	$("#missionStartDate").datepicker({
		dateFormat : 'dd/mm/yy',
		changeMonth : true,
		changeYear : true,
		showAnim : 'fold',
		yearRange : '1950:2050'
	});

	// Creates a date picker in order to select the end date for the mission
	$("#missionEndDate").datepicker({
		dateFormat : 'dd/mm/yy',
		changeMonth : true,
		changeYear : true,
		showAnim : 'fold',
		yearRange : '1950:2050'
	});

	// A a first skill input (by default)
	addMissionSkillInput();

	// Finally open the panel
	$("#add_mission_panel").dialog({
		title : 'Ajouter une mission au profil',
		width : 620,
		close : function(event, ui) {
			clearAddMissionForm();
		}
	});

	// Init the rich editor
	$("#missionDescription").htmlarea();
}

/**
 * Add a skill input into the add mission panel
 */

function addMissionSkillInput() {
	var counter = new Object();
	counter.value = missionInputID;
	addSkillInput(selectedMissionSkills, "missionSkillInput_", counter,
			"mission_skills_panel");
	missionInputID = counter.value;
}

/**
 * Remove an skill input and delete the potential value in the skill list
 */

function removeMissionSkillInput() {
	var counter = new Object();
	counter.value = missionInputID;
	removeSkillInput(selectedMissionSkills, counter, "missionSkillInput_",
			"mission_skills_panel");
	missionInputID = counter.value;
}

/**
 * Clear the add mission form
 */

function clearAddMissionForm() {
	$('#missionTitle').val("");
	$('#missionStartDate').val("");
	$('#missionEndDate').val("");
	$('#missionDescription').val("");
	$('input[id^="missionSkillInput"]').prev().remove();
	$('input[id^="missionSkillInput"]').remove();
	missionInputID = -1;
	selectedMissionSkills = [];
}

/**
 * Submit the add mission form in POST method and send information to controller
 */

function submitMissionForm() {

	var inputName;
	// Iterate over the add mission skills panel children and retrieves their
	// contents
	for ( var i = 0; i <= missionInputID; i++) {
		inputName = "missionSkillInput_" + i;
		selectedMissionSkills[i] = $('#mission_skills_panel > #' + inputName)
				.val();
	}

	var selSkills = selectedMissionSkills.join("|");
	$("#missionSkillsList").val(selSkills);
	$("#addMissionForm").submit();
}

/**
 * Delete a mission
 * 
 * @param missionID
 *            The id of the mission to delete
 */

function deleteMission(missionID, missionTitle) {
	$(
			"<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>Voulez vous supprimer la mission : '"
					+ missionTitle + "' ?</p>").dialog({
		resizable : true,
		modal : true,
		buttons : {
			"Valider" : function() {
				$('<p></p>').load(delMission({
					missionID : missionID
				}), function() {
					alert("La mission à été supprimée !");
					window.location.reload();
				});
			},
			"Annuler" : function() {
				$(this).dialog("close");
			}
		}
	});
}

// 2. EDIT MODE

/**
 * Show the update mission panel (and fill inputs with mission data)
 * 
 * @param missionID
 */

function showUpdateMissionPanel(missionID) {

	$.getJSON("missionToJSON", {
		missionID : missionID,
	}, function(data) {

		// Set the hidden field value (to JAVA side)
		$("#updateMissionForm > #updateMissionID").val(missionID);

		// Mission title and associated client
		$("#updateMissionForm > #updateMissionTitle").val(data.title);
		$("#updateMissionForm > #updateClientID").val(data.client.clientID);

		// Date pickers management
		$("#updateMissionForm > #updateMissionStartDate").datepicker({
			dateFormat : 'dd/mm/yy',
			changeMonth : true,
			changeYear : true,
			showAnim : 'fold',
			yearRange : '1950:2050'
		});

		$("#updateMissionForm > #updateMissionEndDate").datepicker({
			dateFormat : 'dd/mm/yy',
			changeMonth : true,
			changeYear : true,
			showAnim : 'fold',
			yearRange : '1950:2050'
		});
		$("#updateMissionForm > #updateMissionStartDate").datepicker("setDate",
				new Date(data.startDate));
		$("#updateMissionForm > #updateMissionEndDate").datepicker("setDate",
				new Date(data.endDate));

		// Fill description field
		$("#updateMissionDescription").text(data.description);

		// Skills management
		var updateSkills = data.missionSkills;
		for ( var int = 0; int < updateSkills.length; int++) {
			addUpdateMissionSkillInput();
			$("#missionUpdateSkillInput_" + missionUpdateInputID).val(
					updateSkills[int].name);
		}

		// Show the dialog
		$("#update_mission_panel").dialog({
			title : 'Modifier Mission',
			width : 620,
			close : function(event, ui) {
				clearUpdateMissionForm();
			}
		});

		// Init the rich editor
		$("#updateMissionDescription").htmlarea();
	});
}

/**
 * Add a skill input into the mission update form
 */

function addUpdateMissionSkillInput() {
	var counter = new Object();
	counter.value = missionUpdateInputID;
	addSkillInput(selectedUpdateMissionSkills, "missionUpdateSkillInput_",
			counter, "update_mission_skills_panel");
	missionUpdateInputID = counter.value;
}

/**
 * Remove a skill input into the mission update form
 */

function removeUpdateMissionSkillInput() {
	var counter = new Object();
	counter.value = missionUpdateInputID;
	removeSkillInput(selectedUpdateMissionSkills, counter,
			"missionUpdateSkillInput_", "update_mission_skills_panel");
	missionUpdateInputID = counter.value;
}

/**
 * Clear the mission update form
 */

function clearUpdateMissionForm() {
	$("#updateMissionForm > #updateMissionID").val("");
	$("#updateMissionForm > #updateMissionTitle").val("");
	$("#updateMissionForm > #updateClientID").val("");
	$("#updateMissionForm > #updateMissionStartDate").val("");
	$("#updateMissionForm > #updateMissionEndDate").val("");
	$("#updateMissionForm > #updateMissionDescription").val("");
	$('#update_mission_skills_panel > input[id^="missionUpdateSkillInput_"]')
			.prev().remove();
	$('#update_mission_skills_panel > input[id^="missionUpdateSkillInput_"]')
			.remove();
	missionUpdateInputID = -1;
	selectedUpdateMissionSkills = [];
}

/**
 * Launch update
 */

function updateMission() {

	var inputName;
	// Iterate over the update mission skills panel children and retrieves their
	// contents
	for ( var i = 0; i <= missionUpdateInputID; i++) {
		inputName = "missionUpdateSkillInput_" + i;
		selectedUpdateMissionSkills[i] = $(
				'#update_mission_skills_panel > #' + inputName).val();
	}

	var selSkills = selectedUpdateMissionSkills.join("|");
	$("#updateMissionSkillsList").val(selSkills);
	$("#updateMissionForm").submit();
}
